package com.tdam2012.grupo04;

import java.util.ArrayList;


import android.os.Bundle;
import android.app.ListActivity;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;

public class EmailListActivity extends ListActivity implements OnClickListener, OnItemClickListener {

	public static final String KEY_CONTACT_EMAIL = "Contact Email";
	public static final String KEY_CONTACT = "Contact";
	
	private EmailsAdapter _adapter;
	private Contact contacto;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_email_list);
        formatListView();
        cargarEmails();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        return true;
    }

    
    private void formatListView() {
		_adapter = new EmailsAdapter(); 
		getListView().setAdapter(_adapter); 
		getListView().setOnItemClickListener(this);
		
	}
    
    private void onItemSelected(int position) {
		// TODO Auto-generated method stub
    	String email =(String) _adapter.getItem(position);
    	if(email != null){
		Intent i = new Intent(this,MailActivity.class);
		i.putExtra(KEY_CONTACT_EMAIL, email);
		i.putExtra(KEY_CONTACT, contacto);
		startActivity(i);
    	}
	}
    
    private void cargarEmails() {
    	 contacto = (Contact) getIntent().getSerializableExtra(ContactScreenActivity.KEY_CONTACT);
    	_adapter.setEmails(contacto.get_emails());
    	_adapter.notifyDataSetChanged();
			
		}
    
    @Override
    protected void onResume() {
    	// TODO Auto-generated method stub
    	super.onResume();
    }
    
    class Holder {
		TextView email;
		TextView texto;
		ImageView imagen;
	}
    
    

	
	public class EmailsAdapter extends BaseAdapter{
		
		private LayoutInflater _inflater;
		private ArrayList<String> _emails; 

		public EmailsAdapter() { 
			_emails = new ArrayList<String>(); 
			_inflater = LayoutInflater.from(EmailListActivity.this);
		}
		
		
		public void setEmails(ArrayList<String> emails) {
			for (String email : emails) {
				_emails.add(email);
			}
		}
		
		
		public int getCount() {
			// TODO Auto-generated method stub
			return _emails.size();
		}

		@Override
		public Object getItem(int position) {
			// TODO Auto-generated method stub
			return _emails.get(position);
		}

		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return position ;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			// TODO Auto-generated method stub
			Holder holder = null;
			if (convertView == null) {
				convertView = _inflater.inflate(R.layout.attr_item, null); 
				holder = new Holder();  
				holder.texto = (TextView) convertView.findViewById(R.id.txtTitulo);
				holder.email = (TextView) convertView.findViewById(R.id.txtDato);
				holder.imagen=(ImageView) convertView.findViewById(R.id.imagen);
				
				convertView.setTag(holder);
			} else {
				holder = (Holder) convertView.getTag(); 
			}

			

			String email = _emails.get(position);
			String texto = getString(R.string.MessageListActivity_EnviarA) ;
            
            holder.texto.setText(texto);
			holder.email.setText(email);
			holder.imagen.setImageResource(android.R.drawable.ic_menu_send);
			
			return convertView; 
			
		}
			
    	
    }
	
	@Override
	public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
		onItemSelected(position);
		
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		
	}
}
